package com.applet.service.impl;

import com.applet.common.constant.ResultConstant;
import com.applet.common.constant.StateConstant;
import com.applet.common.context.BaseContext;
import com.applet.common.exection.NumberIsFullException;
import com.applet.mapper.ActivityMapper;
import com.applet.mapper.ClassifyMapper;
import com.applet.mapper.UserActivityMapper;
import com.applet.mapper.UserMapper;
import com.applet.pojo.dto.ActivityStatusDTO;
import com.applet.pojo.dto.PublishActivityDTO;
import com.applet.pojo.entity.Activity;
import com.applet.pojo.entity.User;
import com.applet.pojo.entity.UserActivity;
import com.applet.pojo.vo.ActivityDetailsVO;
import com.applet.pojo.vo.ActivityItemVO;
import com.applet.pojo.dto.UpdateActivityDTO;
import com.applet.pojo.vo.SignActivityDetailVo;
import com.applet.service.ActivityService;
import com.applet.service.UserActivityService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author “XuShao”
 * @date 2024/12/15 17:43
 * @description
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserActivityMapper userActivityMapper;

    @Autowired
    private UserActivityService userActivityService;

    @Autowired
    private ClassifyMapper classifyMapper;

    @Override
    public ActivityDetailsVO selectActivityDetails(String id) {
        Activity activity = activityMapper.selectOne(new LambdaQueryWrapper<Activity>().eq(Activity::getId, id).eq(Activity::getDeleted, "0"));

        ActivityDetailsVO detailsVO = new ActivityDetailsVO();

        BeanUtils.copyProperties(activity, detailsVO);

        return detailsVO;
    }

    @Transactional
    @Override
    public void addActivity(PublishActivityDTO publishActivityDTO) {
        Activity activity = new Activity();

        BeanUtils.copyProperties(publishActivityDTO, activity);

        activity.setUserId(BaseContext.getCurrentId());
        activity.setState(Activity.HAVE_NOT_STARTED);
        activity.setNumberOfParticipants(0);
        activity.setDeleted(StateConstant.ENABLE);
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());

        activityMapper.insert(activity);
    }

    @Override
    public List<ActivityItemVO> getAllActivity() {

        return activityMapper.getAllActivity();
    }

    @Transactional
    @Override
    public void updateActivity(UpdateActivityDTO updateActivityDTO) {
        Activity activity = new Activity();
        BeanUtils.copyProperties(updateActivityDTO, activity);
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.updateById(activity);
    }

    @Override
    public List<ActivityItemVO> getUserPublishActivity() {
        return activityMapper.getUserPublishActivity(BaseContext.getCurrentId());
    }

    @Override
    public SignActivityDetailVo getActDetail(String id) {
        userActivityMapper.insert(new UserActivity(id, BaseContext.getCurrentId(), (short) 0, (short) 0));
        Activity activity = activityMapper.selectById(id);
        SignActivityDetailVo signActivityDetailVo = new SignActivityDetailVo();
        BeanUtils.copyProperties(activity, signActivityDetailVo);

        String classifyTitle = classifyMapper.selectById(activity.getClassifyId()).getTitle();
        signActivityDetailVo.setClassifyName(classifyTitle);


        LambdaQueryWrapper<UserActivity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserActivity::getActivityId, id);
        queryWrapper.eq(UserActivity::getUserId, BaseContext.getCurrentId());
        UserActivity userActivity = userActivityMapper.selectOne(queryWrapper);

        String contactName = activity.getContactName();
        signActivityDetailVo.setPublisher(contactName);
        signActivityDetailVo.setCollectStatus(userActivity.getCollect());
        signActivityDetailVo.setSignStatus(userActivity.getIsSign());

        return signActivityDetailVo;
    }

    @Override
    public void updateStatusById(String id, ActivityStatusDTO statusDTO) {
        if (statusDTO != null) {
            LambdaUpdateWrapper<UserActivity> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(UserActivity::getActivityId, id);
            updateWrapper.eq(UserActivity::getUserId, BaseContext.getCurrentId());
            updateWrapper.set(UserActivity::getCollect, statusDTO.getCollectStatus());
            Activity activity = activityMapper.selectById(id);
            if (activity.getNumberOfPeopleUpperLimit() <= activity.getNumberOfParticipants()) {
                // 当前报名人数超过人数上限时,抛出 当前报名人数已满 异常
                throw new NumberIsFullException(ResultConstant.NUMBER_OF_APPLICANTS_IS_FULL);
            }

            updateWrapper.set(UserActivity::getIsSign, statusDTO.getSignStatus());
            activity.setNumberOfParticipants(activity.getNumberOfParticipants() + 1);

            userActivityService.update(updateWrapper);
        }

        userActivityMapper.insert(new UserActivity(id, BaseContext.getCurrentId(), (short) 0, (short) 0));

    }

    @Override
    public List<ActivityItemVO> registeredActivities(String currentUserId) {
        return activityMapper.registeredActivities(currentUserId);
    }

    @Override
    public List<ActivityItemVO> collectedActivities(String currentUserId) {
        return activityMapper.collectedActivities(currentUserId);
    }

}
